<template>
	<view class="inviteFriends">
		<view class="backImg">
			<view class="backImg-left">
				<view @tap="back" class="backImg-left">
					<image src="@/static/user/back.png" mode="widthFix"></image>
				</view>
			</view>
			<view>邀请码</view>
			<view class="backImg-right"></view>
		</view>
		<view class="main" id="share">
			<view class="content">
				<view class="inputBox">
					<input type="text" v-model="referralCode" placeholder="请输入邀请码" class="inputStyle" @blur="getChannelCode">
				</view>
				<view class="inputBox">
					<input type="text" v-model="agentChannelCode" placeholder="请输入服务代码" class="inputStyle" :disabled="isShow">
				</view>
				<view class="submit-btn" @tap="submitFn">确定</view>
<!-- 				<view class="title">温馨提示</view>
				<view class="text">温馨提示温馨提示温馨提示温馨提示温馨提示温馨提示温馨提示</view> -->
			</view>
		</view>

		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';
	import {
		getUsedCode,
		getReferralCode,
		setReferralCode,
		getByAgentChannelCode,
	} from '@/api/user.js';
	import {
		getUser
	} from '@/api/login.js'
	export default {
		data() {
			return {
				modeShow: false,
				imgBlobData: "",
				codeStr: "",
				paddingTop: 0,
				referralCode:"",
				agentChannelCode:"",
				userDetails:{},
				isShow:false
			}
		},
		async onShow() {
			let res = await getUser()
			if (res.status == 200) {
				this.userDetails = res.data.user;
				// if(this.userDetails.referralCode){
				// 	this.referralCode = this.userDetails.referralCode
				// }
				if(this.userDetails.agentChannelCode){
					this.agentChannelCode = this.userDetails.agentChannelCode
				}
			}
		},
		onLoad() {
			let that = this
			uni.getSystemInfo({
				success: function(res) {
					let {
						windowHeight
					} = res
					that.$nextTick(() =>{
						that.paddingTop = windowHeight - 608;
						console.log(that.paddingTop, '0000')
					})
				}
			})
		},
		methods: {
			getCode() {
				getUsedCode({}).then(res => {
					if (res.data.success) {
						let arr = [];
						res.data.obj.forEach(im => {
							arr.push(im.id);
						});
						getReferralCode({
							ids: arr.join(",")
						}).then(res => {
							if (res.data.success) {
								this.codeStr = res.data.obj.referralCode;
								this.$nextTick(() => {
									this.shareFx();
								});
							} else {
								this.modeShow = true;
							}
						});
					}
				});
			},
			getChannelCode(){
				if(this.referralCode){
					getByAgentChannelCode({referralCode:this.referralCode}).then(res =>{
						if(res.data.success){
							this.agentChannelCode = res.data.obj;
							this.isShow = true
						}
					})
				}
			},
			isStyle(data, izSellOut) {
				return {
					'paddingTop': this.paddingTop+'px'
				}
			},
			submitFn(){
				if(this.referralCode == ''||!this.referralCode){
					this.$u.toast('请输入邀请码');
					return;
				}
				if(this.agentChannelCode == '' ||!this.agentChannelCode){
					this.$u.toast('请输入服务代码');
					return;
				}
				setReferralCode({referralCode:this.referralCode,agentChannelCode:this.agentChannelCode}).then(res =>{
					if(res.data.success){
						this.$u.toast('补录成功！');
						setTimeout(() =>{
							this.back();
						},1500)
					}else{
						this.$u.toast(res.data.msg);
					}
				})
			},
			copy() {
				uni.setClipboardData({
					data: this.codeStr,
					success: () => {
						this.$u.toast('复制成功！');
					}
				});
			},
			back() {
				let canNavBack = getCurrentPages();
				if (canNavBack && canNavBack.length > 1) {
					uni.navigateBack({
						delta: 1
					});
				} else {
					history.back();
				}
			},
		},
		computed: {
			...mapState(["hasLogin", "uerInfo"])
		},
	}
</script>

<style lang="less" scoped>
	page {
		width: 100%;
		height: 100%;
		min-height: 100vh;
	}

	.inviteFriends {
		width: 100%;
		height: 100%;
		min-height: 100vh;
		background-image: url('@/static/user/inviteFriends.png');
		background-size: 100% 100%;
		background-repeat: no-repeat;

		.register-success {
			width: 100%;
			height: 360upx;
			padding: 0 32upx;

			.bottm {
				text-align: center;
				color: #fff;
				font-size: 24upx;
				height: 60upx;
				line-height: 60upx;
				margin-top: 120upx;
				background-color: #2b267f;
				border-radius: 10upx;
			}

			.title {
				margin-top: 80upx;
				font-size: 40upx;
				text-align: center;
				color: #2C1769;
			}
		}

		.inviteFriendsImg {
			width: 100%;
			height: 100%;
			position: absolute;
			top: 0;
			left: 0;
			z-index: 2;
		}

		.main {
			width: 100%;
			height: 100%;
			padding-top: 340upx;

			.content {
				border-radius: 20upx;
				width: 100%;
				padding: 150upx 82upx;
				background-image: url('@/static/user/inviteFriendsKuang.png');
				background-size: 100% 100%;
				background-repeat: no-repeat;

				.inputBox {
					width: 100%;
					height: 88upx;
					border: 1px solid #E1E1E1;
					border-radius: 80upx;
					margin-bottom: 20upx;

					.inputStyle {
						width: 100%;
						height: 100%;
						display: flex;
						align-items: center;
						justify-content: center;
						text-align: center;
					}

				}

				.submit-btn {
					width: 100%;
					margin-top: 40upx;
					height: 92upx;
					background: #F7CF67;
					border-radius: 92upx;
					display: flex;
					align-items: center;
					justify-content: center;
					font-size: 30upx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #FFFFFF;
				}

				.title {
					font-size: 30upx;
					font-family: PingFang SC;
					font-weight: bold;
					color: #000000;
					margin-top: 172upx;
				}

				.text {
					font-size: 24upx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #616161;
					line-height: 40upx;
					text-indent: 1cm;
				}
			}
		}

		.preservation {
			width: 100%;
			height: 80upx;
			border-radius: 40upx;
			position: fixed;
			left: 0;
			bottom: 40upx;
			padding: 0 60upx;
			z-index: 3;

			button {
				width: 100%;
				height: 100%;
				background: linear-gradient(95deg, #FA542E, #D30211);
				border-radius: 40upx;
				padding: 0;
				font-size: 32upx;
				line-height: 80upx;
				border: 0 none;
				color: #fff;
			}

			button::after {
				border: 0 none;
			}
		}
	}

	.backImg {
		width: 100%;
		position: fixed;
		top: 0;
		left: 0;
		z-index: 99;
		padding: 40upx;
		display: flex;
		justify-content: space-between;
		color: #000;
		font-size: 32upx;
		font-family: PingFang SC;
		font-weight: bold;

		.backImg-left {
			display: flex;
			align-items: center;

			image {
				width: 20upx;
				height: 45upx;
			}
		}

		.backImg-right {
			display: flex;
			justify-content: flex-end;
			align-items: center;

			image {
				width: 44upx;
				height: 36upx;
				margin-left: 10upx;
			}

			.share {
				width: 36upx;
				height: 36upx;
			}
		}
	}
</style>
